package com.gln.bloger.util;

import java.util.ArrayList;
import java.util.List;

public class PagingSupport <T>{
	// ---------------------------------------------------------
	// static variables
	// ---------------------------------------------------------
	// 默认每页显示条数
	public static final int DEFAULT_MAX_PAGE_ITEMS = 10;

	// ---------------------------------------------------------
	// memeber variables
	// ---------------------------------------------------------

	// 每页显示条数
	private int maxPageItems = DEFAULT_MAX_PAGE_ITEMS;

	private List<T> data = new ArrayList<T>();
	//current page index
	private int currentPage;
	//pre page index
	private int prePage;
	//next page index
	private int nextPage;
	// total items's count
	private int totalCount;
	//is first page
	private Boolean isFirst;
	//is last page
	private Boolean isLast;
	//total page count
	private int totalPageCount;

	/*
	 * index
	 */
	private int [] index ;
	public int[] getIndex() {
		return index;
	}

	public void setIndex(int[] index) {
		this.index = index;
	}

	public int getTotalPageCount() {
		return totalPageCount;
	}

	public void setTotalPageCount(int totalPageCount) {
		this.totalPageCount = totalPageCount;
	}

	public int getMaxPageItems() {
		return maxPageItems;
	}

	public void setMaxPageItems(int maxPageItems) {
		this.maxPageItems = maxPageItems;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public Boolean getIsFirst() {
		return isFirst;
	}

	public void setFirst(boolean isFirst) {
		this.isFirst = isFirst;
	}

	public Boolean getIsLast() {
		return isLast;
	}

	public void setLast(boolean isLast) {
		this.isLast = isLast;
	}

	/**
	 * set data
	 * @param data
	 */
	public void setData(List<T> data){
		this.data = data;
		this.totalCount = data.size();
		
		if(totalCount < maxPageItems){
			totalPageCount = 1;
		}else if(totalCount%maxPageItems == 0){
			totalPageCount = totalCount/maxPageItems;
		}else{
			totalPageCount = totalCount/maxPageItems + 1;
		}
		index = new int[totalPageCount];
		for(int i = 0; i<totalPageCount; i++){
			index[i] = i+1;
		}
		setCurrentPage(1);
	}
	
	public List<T> getData(){
		List<T> showedData = new ArrayList<T>();
		for(int i = (currentPage-1)*maxPageItems; 
		     i<currentPage*maxPageItems && i<data.size(); i++){
			showedData.add(data.get(i));
		}
		return showedData;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
		if(currentPage == 1){
			isFirst = Boolean.TRUE;
		}else{
			isFirst = Boolean.FALSE;
		}
		if(currentPage == totalPageCount){
			isLast = Boolean.TRUE;
		}else{
			isLast = Boolean.FALSE;
		}
		if(currentPage > 1){
			prePage = currentPage-1;
		}
		if(currentPage < totalPageCount){
			nextPage = currentPage + 1;
		}
	}

	public int getPrePage() {
		return prePage;
	}

	public void setPrePage(int prePage) {
		this.prePage = prePage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}
}
